<template>
    <!-- 搜索结果 -->
    <div class="search-result">
        <van-list v-model="loading" :finished="finished" :error.sync="error" error-text="请求失败，点击重新加载" finished-text="没有更多了" @load="onLoad">
            <van-cell v-for="(article,index) in list" :key="index" :title="article.title" />
        </van-list>
    </div>
</template>

<script>
import { getSearchResult } from "@/api/search.js";
export default {
  props: {
    searchText: String,
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 20,
      error: false,
    };
  },
  methods: {
    async onLoad() {
      try {
        const { data } = await getSearchResult({
          page: this.page,
          per_page: this.per_page,
          q: this.searchText,
        });
        if (Math.random() > 0.5) {
          JSON.parse("assas");
        }
        const { results } = data.data;
        this.list.push(...results);
        this.loading = false;
        if (results.length) {
          this.page++;
        } else {
          this.finished = true;
        }
      } catch (error) {
        this.error = true;
        this.loading = false;
        this.$toast("获取数据失败，请稍后重试");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>